$(function(){
	init();
});

init=function(){
	initDatepicker();
	initAutocomplete();
	initSubmitButton();
};

var citites= [
"包头","北海","北京","长春","常德","长沙",
"常州","成都","赤峰","重庆","大理","大连",
"丹东","福州","赣州","广州","桂林","贵阳",
"海口","海拉尔","杭州","哈尔滨","合肥","呼和浩特",
"黄山","黄岩","佳木斯","吉林","济南","景德镇",
"晋江","九江","昆明","兰州","拉萨","连云港",
"丽江","南宁","南通","南阳","宁波","青岛",
"秦皇岛","齐齐哈尔","衢州","三亚","上海",
"汕头","沈阳","石家庄","太原","天津","乌鲁木齐",
"万州","温州","武汉","武夷山","厦门","西安",
"襄樊","锡林浩特","西宁","西双版纳","延安", 
"徐州","延吉","烟台","宜宾","宜昌","银川", 
"伊宁","义乌","张家界","柳州","洛阳","泸州", 
"芒市","临沂","牡丹江","南昌","南京","湛江", 
"郑州","舟山","珠海","香港","澳门","深圳"];

initDatepicker = function() {
	var dates = null;
	dates = $('#startDate,#endDate').datepicker({
		dateFormat : "yy-mm-dd",
		minDate: 0,
		onSelect: function( selectedDate ) {
			var option = this.id == "startDate" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" ),
				date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings );
			dates.not( this ).datepicker( "option", option, date );
		}
	});
};

initAutocomplete=function(){
	$( "#depCity,#arrCity" ).autocomplete({
		source: citites
	});
};

initSubmitButton=function(){
	$('#openFormSubmit').click(function(){
		$.common.btnStatus($(this), true, 'Submiting...');
		submitForm();
		return false;
	});
};

submitForm=function() {
	url = $('#uri').val();
	if ('#' === url) {
		alert('Sorry, not support yet!');
		$.common.btnStatus($('#openFormSubmit'), false, 'Submit');
		return false;
	}
	params = $('#openForm').formSerialize();
	accpet = $('#accpet').val();
	$.common.before(url, params);
	$.common.ajax(url, params, accpet, $.common.success, $.common.error);
};

$.common = {
	ajax: function(url, params, accpet, su, er) {
		$.ajax({
			type: "post",
			url: url,
			data: params,
			dataType: accpet,
			success: su,
			error: er
		});
	},
	before : function(url, params) {
		$('#o_uri').val(BASE + '/' + url + '?' + params);
	},
	success : function(data) {
		$.common.btnStatus($('#openFormSubmit'), false, 'Submit');
		if ('json' === $('#accpet').val()) {
			$('#o_result').val(JsonUtil.json2String(data));
		} else {
			$('#o_result').val(XmlUtils.xml2String(data));
		}
	},
	error: function(data){
		$('#o_result').val(data.responseText);
	},
	btnStatus: function(obj, b, v) {
		obj.attr('disabled',b).val(v);
	}
};
